package com.peng.crm.workbench.controller;

import com.peng.crm.commons.utils.DateUtil;
import com.peng.crm.commons.utils.Result;
import com.peng.crm.commons.utils.UUIDUtil;
import com.peng.crm.settings.entity.User;
import com.peng.crm.workbench.entity.ClueRemark;
import com.peng.crm.workbench.service.ClueRemarkService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 线索备注(ClueRemark)表控制层
 *
 * @author makejava
 * @since 2022-07-02 19:11:30
 */
@RestController
@RequestMapping("/clueRemark")
public class ClueRemarkController {
    /**
     * 服务对象
     */
    @Resource
    private ClueRemarkService clueRemarkService;

    /**
     * 通过主键查询备注信息回显数据用
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/find/{id}")
    public Result  selectById(@PathVariable("id") String id) {
        return Result.success(clueRemarkService.selectById(id));
    }


    /**
     * 查询所有备注信息
     * @param clueId 线索id
     * @return 查询结果
     */
    @GetMapping("/list")
    public Result list(String clueId) {
        return Result.success(clueRemarkService.selectList(clueId));
    }


    /**
     * 新增数据
     *
     * @param clueRemark 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public Result  add(@RequestBody ClueRemark clueRemark, HttpSession session) {
        User loginUser = (User)session.getAttribute("loginUser");
        //手动维护ID
        clueRemark.setId(UUIDUtil.getUUID());
        //设置创建者，修改者
        clueRemark.setCreateBy(loginUser.getId());
        clueRemark.setEditBy(loginUser.getId());
        //设置创建时间，修改时间
        clueRemark.setCreateTime(DateUtil.formatDate(new Date()));
        clueRemark.setEditTime(DateUtil.formatDate(new Date()));
        return Result.success(clueRemarkService.insert(clueRemark));
    }

    /**
     * 编辑数据
     *
     * @param clueRemark 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    public Result  edit(@RequestBody ClueRemark clueRemark,HttpSession session) {
        //从session域中取到当前登陆的用户
        User loginUser = (User)session.getAttribute("loginUser");
        //设置修改者
        clueRemark.setEditBy(loginUser.getLoginAct());
        //设置修改时间
        clueRemark.setEditTime(DateUtil.formatDate(new Date()));
        return Result.success(clueRemarkService.update(clueRemark));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") String id) {
        return Result.success(clueRemarkService.deleteById(id));
    }


}

